package com.duoec.magichour.service.impl;

import com.duoec.magichour.dto.response.ProductSimpleInfoDto;
import com.duoec.magichour.mapper.ProductMapper;
import com.duoec.magichour.pojo.Product;
import com.duoec.magichour.service.ProductService;
import com.google.common.collect.Lists;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by ycoe on 17/6/26.
 */
@Service
public class ProductServiceImpl implements ProductService {
    @Autowired
    private ProductMapper productMapper;

    @Override
    public List<ProductSimpleInfoDto> searchProducts(long userId, String keyword, int pageNo, int pageSize) {
        List<ProductSimpleInfoDto> productViewList = Lists.newArrayList();
        List<Product> productList = productMapper.search(userId, keyword, (pageNo - 1) * pageSize, pageSize);
        if (productList == null || productList.isEmpty()) {
            return productViewList;
        }
        productList.forEach(product -> {
            ProductSimpleInfoDto item = new ProductSimpleInfoDto();
            BeanUtils.copyProperties(product, item);

            productViewList.add(item);
        });

        return productViewList;
    }

    @Override
    public List<ProductSimpleInfoDto> getSimpleInfoByIds(List<Long> productIds) {
        List<Product> products = productMapper.getByIds(productIds);
        if (products == null || products.isEmpty()) {
            return null;
        }
        List<ProductSimpleInfoDto> productSimpleList = Lists.newArrayList();
        products.forEach(product -> {
            ProductSimpleInfoDto item = new ProductSimpleInfoDto();
            BeanUtils.copyProperties(product, item);

            productSimpleList.add(item);
        });
        return productSimpleList;
    }
}
